from typing import Optional, List
from datetime import datetime
from pydantic import BaseModel, EmailStr
from enums import UserStatusEnum


class UserForm(BaseModel):
    nickname: str
    username: str
    password: str
    avatar: Optional[str] = None
    permissions: Optional[List[str]] = []
    roles: Optional[List[int]] = []
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None


class UserPublicInfo(BaseModel):
    id: int
    nickname: str
    username: str
    avatar: Optional[str] = None
    last_login_time: Optional[datetime] = None
    permissions: Optional[List[int]] = []
    roles: Optional[List[int]] = []
    status: UserStatusEnum
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None

    class Config:
        from_attributes = True


class UserModel(BaseModel):
    id: int
    nickname: str
    username: str
    password: str
    avatar: Optional[str] = None
    email: Optional[EmailStr] = None
    last_login_time: Optional[datetime] = None
    permissions: Optional[List[int]] = []
    roles: Optional[List[int]] = []
    status: UserStatusEnum
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None

    class Config:
        from_attributes = True


class CreateUserRequest(UserForm):
    pass


class AdminChangePasswordRequest(BaseModel):
    id: int
    password: str


class ChangePasswordRequest(BaseModel):
    old_password: str
    password: str


class ChangeUserInfoRequest(BaseModel):
    nickname: str
    avatar: Optional[str] = None


class UserRoleForm(BaseModel):
    id: Optional[int] = None
    name: str
    permissions: Optional[List[int]] = []
    created_by: Optional[int] = None
    updated_by: Optional[int] = None


class UserRoleModel(BaseModel):
    id: int
    name: str
    permissions: Optional[List[int]] = []
    status: int
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None

    class Config:
        from_attributes = True


class UserRolePagination(BaseModel):
    page: int
    per_page: int
    total: int
    data: List[UserRoleModel]


class GetRegisterVerifyCodeRequest(BaseModel):
    email: EmailStr


class UserLoginRequest(BaseModel):
    email: str
    password: str


class UserLoginResponse(BaseModel):
    id: int
    nickname: str
    username: str
    avatar: Optional[str] = None
    last_login_time: Optional[datetime] = None
    permissions: Optional[List[int]] = []
    roles: Optional[List[int]] = []
    access_token: str
    refresh_token: Optional[str] = None
    expires: Optional[int] = None
    token_type: str


class RefreshTokenRequest(BaseModel):
    refresh_token: str


class RefreshTokenResponse(BaseModel):
    access_token: str
    refresh_token: str
    expires: int


class UsersPagination(BaseModel):
    page: int
    page_size: int
    total: int
    data: List[UserPublicInfo]
